<?php
declare(strict_types=1);

/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Jobs;

use Qiniu\Auth;
use Hyperf\AsyncQueue\Job;
use Hyperf\DbConnection\Db;

class DelQiniuFile extends Job
{


//    /**
//     * @Inject
//     * @var Sender
//     */
//    protected $sender;

//    use SerializationModel;
//    use Serialization;
    public $params;
    public $ids;




    public $init = false;

    /**^M
     * ............................................................... $maxAttempts+1 ...^M
     *^M
     * @var int^M
     */
    protected $maxAttempts = 0;


    public function __construct($params)
    {
        $ids = data_get($params, 'ids', []);
        $this->ids = $ids;
    }

    public function handle(): void
    {
        $accessKey = env('QINIU_ACCESS_KEY');
        $secretKey = env('QINIU_SECRET_KEY');
        $auth = new Auth($accessKey, $secretKey);
        $config = new \Qiniu\Config();
        $bucketManager = new \Qiniu\Storage\BucketManager($auth, $config);
        foreach ($this->ids as $id) {

            $qiniuFile = DB::table('qiniu_file')
                ->where('id', '=', $id)
                ->first();
            $keys = array(data_get($qiniuFile, 'key'));
            $bucket = data_get($qiniuFile, 'bucket', '');
            $ops = $bucketManager->buildBatchDelete($bucket, $keys);
            list($ret, $err) = $bucketManager->batch($ops);
            if ($err) {
                info('delqnf', [$err]);
            } else {
                info('delqnf', [$ret]);
            }
        }
    }
}
